//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Policy Injection Application Block
//===============================================================================

using System.Configuration;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using EntLibContrib.PolicyInjection.CallHandlers;

namespace EntLibContrib.PolicyInjection.CallHandlers.Configuration
{
	/// <summary>
	/// Thread Safe Call Handler Data
	/// </summary>
	public class ThreadSafeCallHandlerData : CallHandlerData
	{
		#region Constants
		private const string SyncRootPropertyName = "syncRoot";
		#endregion

		#region Properties
		/// <summary>
		/// The SyncRootName to use.
		/// </summary>
		/// <value>The "SyncRootName" attribute in configuration</value>
		[ConfigurationProperty(SyncRootPropertyName)]
		public string SyncRoot
		{
			get { return (string)base[SyncRootPropertyName]; }
			set { base[SyncRootPropertyName] = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Construct a new <see cref="ThreadSafeCallHandlerData"/>.
		/// </summary>
		public ThreadSafeCallHandlerData()
		{
		}

		/// <summary>
		/// Construct a new <see cref="ThreadSafeCallHandlerData"/>.
		/// </summary>
		/// <param name="handlerName">Name of the handler.</param>
		public ThreadSafeCallHandlerData(string handlerName)
			: base(handlerName, typeof(ThreadSafeCallHandler))
		{
		}

		/// <summary>
		/// Construct a new <see cref="ThreadSafeCallHandlerData"/>.
		/// </summary>
		/// <param name="handlerName">Name of the handler.</param>
		/// <param name="handlerOrder">The handler order.</param>
		public ThreadSafeCallHandlerData(string handlerName, int handlerOrder)
			: base(handlerName, typeof(ThreadSafeCallHandler))
		{
			Order = handlerOrder;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Adds the call handler represented by this configuration object to <paramref name="policy"/>.
		/// </summary>
		/// <param name="policy">The policy to which the rule must be added.</param>
		/// <param name="configurationSource">The configuration source from which additional information
		/// can be retrieved, if necessary.</param>
		public override void ConfigurePolicy(PolicyDefinition policy, IConfigurationSource configurationSource)
		{
			policy.AddCallHandler<ThreadSafeCallHandler>(
					new ContainerControlledLifetimeManager(),
					new InjectionConstructor(
							new InjectionParameter<string>(this.SyncRoot),
							new InjectionParameter<int>(this.Order)));
		}
		#endregion
	}
}
